Flutter 应用实现条形码、二维码的扫描与生成 |
您所在的位置:网站首页 › flutter qrcode › Flutter 应用实现条形码、二维码的扫描与生成 |
一、前言
我们开发原生的时候可以使用zxing进行生成条形码与二维码,但是flutter的插件中只找到了二维码的生成与扫描,业务需求里面需要条形码的生成,需要在原有的基础上进行修改。 二、效果图通过MethodChannel调用原生代码,进行效果的实现 const MethodChannel _channel = const MethodChannel(‘qr_scan’) 四、具体实现方案 1.使用的第三方库qrscan: ^0.2.17 2.主要代码 class MyApp extends StatefulWidget { MyApp({Key key}) : super(key: key); _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { String barcode = null; Uint8List bytes = Uint8List(0); @override initState() { super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('二维码生成器'), ), body: Container( margin: EdgeInsets.only(top: 50), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Container( width: 200, height: 200, child: Image.memory(bytes), ), // _createBody(), Text(barcode ?? "请扫描相关的条形码"), MaterialButton( onPressed: scan, child: Text("Scan"), color: Colors.blue, textColor: Colors.white, ), MaterialButton( onPressed: () => _generateBarCode(barcode), child: Text("生成普通二维码"), color: Colors.blue, textColor: Colors.white, ), MaterialButton( onPressed: () => _generateBarCode1(barcode), child: Text("生成条形码"), color: Colors.blue, textColor: Colors.white, ), ], ), ) ), ); } ///扫描二维码 Future scan() async { try { String barcode = await scanner.scan(); print("这是扫描出来的结果"+barcode); setState(() { this.barcode = barcode; bytes=Uint8List(0); // ClipboardData data = new ClipboardData(text:barcode); // Clipboard.setData(data); }); } on Exception catch (e) { if (e == scanner.CameraAccessDenied) { setState(() { this.barcode = 'The user did not grant the camera permission!'; }); } else { setState(() => this.barcode = 'Unknown error: $e'); } } on FormatException { setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)'); } catch (e) { setState(() => this.barcode = 'Unknown error: $e'); } } Future _generateBarCode(String inputCode) async { Uint8List result = await scanner.generateBarCode(inputCode); this.setState(() => this.bytes = result); } Future _generateBarCode1(String inputCode) async { Uint8List result = await scanner.generateBarCode1(inputCode); this.setState(() => this.bytes = result); } } 五、注意事项通过以上操作只能实现扫描与生成二维码的操作,条形码的生成还需要以下步骤进行修改。 1.flutter端添加关键代码 2.Android端添加关键代码 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |